package com.wnnight.wys.mapper.notice;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnnight.wys.entity.notice.Notice;
import com.wnnight.wys.vo.NoticeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: NoticeMapper
 * @Description: //TODO
 * @author: xjh
 * @date: 2022/8/28-21:17
 * @version: v1.0
 */
public interface NoticeMapper extends BaseMapper<Notice> {
    @Select("select * from wys_notice where title like CONCAT('%',#{title},'%') and status='y'")
    @Results(id="noticeResultMap",value=
            {
                @Result(property = "id",column = "id",id = true),
                @Result(property = "title",column = "title"),
                //@Result(property = "noticeType.id",column = "type_id"),
                    //上次编辑到这儿
                @Result(property = "type",column = "type_id",one = @One(select = "com.wnnight.wys.mapper.notice.NoticeTypeMapper.getById")),
                @Result(property = "urgency",column = "urgency"),
                @Result(property = "contentType",column = "content_type"),
                @Result(property = "createTime",column = "create_time"),
                @Result(property = "readCount",column = "read_count"),
                @Result(property = "content",column = "content"),
                @Result(property = "status",column = "status"),
                @Result(property = "staff.id",column = "staff_id")
            }
    )
    List<Notice> getByTitle(String title);//标题模糊查询

    @Select("select * from wys_notice where type_id=#{typeId} and status='y'")
    @ResultMap("noticeResultMap")
    List<Notice> getByTypeId(int typeId);//前端传类型Id

    @Select("select * from wys_notice where id=#{id} and status='y'")
    @ResultMap("noticeResultMap")
    Notice getById(int id);

    //删除
    @Update("update wys_notice set status='n' where id=#{id}")
    void delete(int id);

    //恢复
    @Update("update wys_notice set status='y' where id=#{id}")
    void reset(int id);

    //新增notice
    @Insert("insert into wys_notice (title,type_id,urgency,content_type,create_time,read_count,content,status,staff_id,img,video) " +
            "values(#{title},#{type.id},#{urgency},#{contentType},#{createTime},#{readCount},#{content},#{status},#{staff.id},#{img},#{video})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void add(Notice notice);

    //更新notice，动态sql
    @UpdateProvider(type=SqlProvider.class,method = "update")
    void update(Notice notice);


    //根据起始日期时间查询
    @SelectProvider(type=SqlProvider.class,method = "getByDate")
//    @Select("select * from wys_notice where" +
//            " DATE_FORMAT(create_time, '%Y-%m-%d') " +
//            ">="+
//            "DATE_FORMAT(#{startDate},'%Y-%m-%d') and " +
//            "DATE_FORMAT(create_time, '%Y-%m-%d') <= DATE_FORMAT(#{endDate},'%Y-%m-%d')")
    @ResultMap("noticeResultMap")
    List<Notice> getByStartAndEndDate(@Param("startDate")Date startDate,@Param("endDate")Date endDate);

    //动态sql，模糊查询
    @SelectProvider(type = SqlProvider.class,method = "getByCondition")
    @ResultMap("noticeResultMap")
    List<Notice> getByCondition(Notice notice);

    @Select("select * from wys_notice where status='y'")
    @ResultMap("noticeResultMap")
    List<Notice> getAll();

    @SelectProvider(type = SqlProvider.class,method = "getByCondition2")
    @ResultMap("noticeResultMap")
    List<Notice> getByCondition2(NoticeVo noticeVo);

    @Slf4j
    class SqlProvider {
        //动态更新
        public static String update(final Notice notice) {
            return new SQL(){{
                UPDATE("wys_notice");
                //条件写法.
                if(notice.getTitle() != null){
                    SET("title=#{title}");
                }
                if(notice.getType() != null){
                    SET("type_id=#{type.id}");
                }
                if(notice.getUrgency() != null){
                    SET("urgency=#{urgency}");
                }
                if(notice.getContentType() != null){
                    SET("content_type=#{contentType}");
                }
                if(notice.getCreateTime() != null){
                    SET("create_time=#{createTime}");
                }
                if(notice.getReadCount() != null){
                    SET("read_count=#{readCount}");
                }
                if(notice.getContent() != null){
                    SET("content=#{content}");
                }
                if(notice.getStatus() != null){
                    SET("status=#{status}");
                }
                if(notice.getStaff() != null){
                    SET("staff_id=#{staff.id}");
                }
                if(notice.getImg() != null){
                    SET("img=#{img}");
                }
                if(notice.getVideo() != null){
                    SET("video=#{video}");
                }
                WHERE("id=#{id}");
            }}.toString();
        }
        //条件查询，含标题模糊查询
        public static String getByCondition(final Notice notice) {
            return new SQL() {{
                SELECT("*");
                FROM("wys_notice");
                if(notice.getTitle() != null){
                    WHERE("title like concat('%',#{title},'%')");
                }
                if(notice.getType() != null){
                    WHERE("type_id=#{type.id}");
                }
                if(notice.getUrgency() != null){
                    WHERE("urgency=#{urgency}");
                }
                if(notice.getContentType() != null){
                    WHERE("content_type=#{contentType}");
                }
//                if(notice.getStatus() != null){
//                    WHERE("status='y'");
//                }
                if(notice.getStaff() != null){
                    WHERE("staff_id=#{staff.id}");
                }
                WHERE("status='y'");
            }}.toString();
        }
        //动态sql，根据时间段查询数据
        public static String getByDate( Date startDate,Date endDate) {
            String sql = new SQL() {
                {
                    SELECT("*");
                    FROM("wys_notice");
                    if (startDate != null) {
                        WHERE("DATE_FORMAT(create_time, '%Y-%m-%d') >= DATE_FORMAT(#{startDate},'%Y-%m-%d')");
                    }
                    if(endDate != null){
                        WHERE("DATE_FORMAT(create_time, '%Y-%m-%d') <= DATE_FORMAT(#{endDate},'%Y-%m-%d')");
                    }
                }
            }.toString();
            log.debug("sql:{}",sql);
            return sql;
        }
        public static String getByCondition2(NoticeVo noticeVo) {
            String sql = new SQL() {
                {
                    SELECT("*");
                    FROM("wys_notice");
                    if(noticeVo.getTitle() != null){
                        WHERE("title like concat('%',#{title},'%')");
                    }
                    if(noticeVo.getTypeId() != null){
                        if(noticeVo.getTypeId()==0){
                            WHERE("1=1");
                        }else{
                        WHERE("type_id=#{typeId}");
                        }
                    }
                    if (noticeVo.getStartDate() != null) {
                        WHERE("DATE_FORMAT(create_time, '%Y-%m-%d') >= DATE_FORMAT(#{startDate},'%Y-%m-%d')");
                    }
                    if(noticeVo.getEndDate() != null){
                        WHERE("DATE_FORMAT(create_time, '%Y-%m-%d') <= DATE_FORMAT(#{endDate},'%Y-%m-%d')");
                    }
                }
            }.toString();
            log.debug("sql:{}",sql);
            return sql;
        }
    }


}
